package domeComparator;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DickYang
 * Date: 2023-06-03
 * Time: 12:30
 */
class Student {
    public String name;//姓名
    public int age;//年龄
    public int score;//成绩

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Strudent{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}

class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o2.age - o1.age;
    }
}

class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",12,89);
        students[1] = new Student("abc",23,34);
        students[2] = new Student("lisi",18,78);

        AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();

        System.out.println("按照年龄比较:");
        Arrays.sort(students,ageComparator);
        System.out.println(Arrays.toString(students));

        System.out.println("按照姓名比较:");
        Arrays.sort(students,nameComparator);
        System.out.println(Arrays.toString(students));
    }
}
